package work.linruchang.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.SneakyThrows;
import work.linruchang.util.bean.IDCardInfo;
import work.linruchang.util.bean.ocr.OcrInterface;
import work.linruchang.util.bean.ocr.TelcentOcr;

import java.io.File;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 增强的OCR处理工具
 *
 * 【注意】使用此工具类，必须配置OcrInterface实现类给Bean管理
 * @author LinRuChang
 * @version 1.0
 * @date 2022/03/29
 * @since 1.8
 **/
public class EnhanceOcrUtil {

    /**
     * 正反照识别
     *
     * @param frontCardImgFileStream 正面照身份证图片的输入流 可空
     * @param backCardImgFileStream  反面照身份证图片的输入流 可空
     * @return
     */
    public static IDCardInfo ocrIDCard(InputStream frontCardImgFileStream, InputStream backCardImgFileStream) {
        String frontCardImgBase64 = null;
        String backCardImgBase64 = null;
        if (frontCardImgFileStream != null) {
            frontCardImgBase64 = Base64.encode(frontCardImgFileStream);
        }
        if (backCardImgFileStream != null) {
            backCardImgBase64 = Base64.encode(backCardImgFileStream);
        }
        return ocrIDCard(frontCardImgBase64, backCardImgBase64);
    }


    /**
     * 正反照识别
     *
     * @param frontCardImgFile 正面照身份证图片的路径地址
     * @param backCardImgFile  反面照身份证图片的路径地址
     * @return
     */
    public static IDCardInfo ocrIDCard(File frontCardImgFile, File backCardImgFile) {
        String frontCardImgBase64 = null;
        String backCardImgBase64 = null;

        if (FileUtil.exist(frontCardImgFile)) {
            frontCardImgBase64 = Base64.encode(FileUtil.readBytes(frontCardImgFile));
        }
        if (FileUtil.exist(backCardImgFile)) {
            backCardImgBase64 = Base64.encode(FileUtil.readBytes(backCardImgFile));
        }
        return ocrIDCard(frontCardImgBase64, backCardImgBase64);
    }

    /**
     * 正反照识别
     *
     * @param frontCardImgBase64 正面照身份证图片内容的Base64编码
     * @param backCardImgBase64  反面照身份证图片内容的Base64编码
     * @return
     */
    public static IDCardInfo ocrIDCard(String frontCardImgBase64, String backCardImgBase64) {
        if (!StrUtil.isAllBlank(frontCardImgBase64, backCardImgBase64)) {
            IDCardInfo front = ocrIDCardByCardSide(frontCardImgBase64, "FRONT");
            IDCardInfo back = ocrIDCardByCardSide(backCardImgBase64, "BACK");

            Map<String, Object> frontMap = BeanUtil.beanToMap(front, false, true);
            frontMap = EnhanceCollUtil.filterBlankValue(frontMap);
            //Console.log("身份证正面信息：{}", frontMap);
            Map<String, Object> backMap = BeanUtil.beanToMap(back, false, true);
            backMap = EnhanceCollUtil.filterBlankValue(backMap);
            //Console.log("身份证反面信息：{}", backMap);

            //待处理
            Map result = new HashMap();
            result.putAll(ObjectUtil.defaultIfNull(frontMap, Collections.emptyMap()));
            result.putAll(ObjectUtil.defaultIfNull(backMap, Collections.emptyMap()));
            return BeanUtil.mapToBean(result, IDCardInfo.class, true);
        }
        return null;
    }

    /**
     * 识别身份证信息
     *
     * @param cardImgBase64 身份证图片
     * @param cardSide      身份证的正反面  FRONT正面（头像）  BACK反面（国徽） 不填则第三方OCR自动识别正反照
     * @return
     */
    @SneakyThrows
    public static IDCardInfo ocrIDCardByCardSide(String partyManufacturer, String cardImgBase64, String cardSide) {
        OcrInterface ocrInterface = null;
        if (StrUtil.equals(partyManufacturer, "TelcentOcr")) {
            ocrInterface = SpringUtil.getBean(TelcentOcr.class);
        } else {
            ocrInterface = SpringUtil.getBean(OcrInterface.class);
        }
        Assert.notNull(ocrInterface, "缺失证件解析接口，请检查");
        return ocrInterface.ocrIDCardByCardSide(cardImgBase64, cardSide);
    }

    /**
     * 识别身份证信息
     *
     * @param cardImgBase64 身份证图片
     * @param cardSide      身份证的正反面  FRONT正面（头像）  BACK反面（国徽） 不填则第三方OCR自动识别正反照
     * @return
     */
    @SneakyThrows
    public static IDCardInfo ocrIDCardByCardSide(String cardImgBase64, String cardSide) {
        return ocrIDCardByCardSide(null, cardImgBase64, cardSide);
    }


}
